<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="视频原始名称" prop="videoOriginalName">
      <el-input v-model="dataForm.videoOriginalName" placeholder="视频原始名称"></el-input>
    </el-form-item>
    <el-form-item label="节点标题" prop="title">
      <el-input v-model="dataForm.title" placeholder="节点标题"></el-input>
    </el-form-item>
    <el-form-item label="课程id" prop="courseId">
      <el-input v-model="dataForm.courseId" placeholder="课程id"></el-input>
    </el-form-item>
    <el-form-item label="课程目录id" prop="catalogId">
      <el-input v-model="dataForm.catalogId" placeholder="课程目录id"></el-input>
    </el-form-item>
    <el-form-item label="云端视频资源id" prop="videoSourceId">
      <el-input v-model="dataForm.videoSourceId" placeholder="云端视频资源id"></el-input>
    </el-form-item>
    <el-form-item label="播放时长" prop="duration">
      <el-input v-model="dataForm.duration" placeholder="播放时长"></el-input>
    </el-form-item>
    <el-form-item label="播放次数" prop="playCount">
      <el-input v-model="dataForm.playCount" placeholder="播放次数"></el-input>
    </el-form-item>
    <el-form-item label="是否可以试听【0代表免费，1代表收费】" prop="isFree">
      <el-input v-model="dataForm.isFree" placeholder="是否可以试听【0代表免费，1代表收费】"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="修改时间" prop="modifyTime">
      <el-input v-model="dataForm.modifyTime" placeholder="修改时间"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          videoOriginalName: '',
          title: '',
          courseId: '',
          catalogId: '',
          videoSourceId: '',
          duration: '',
          playCount: '',
          isFree: '',
          createTime: '',
          modifyTime: ''
        },
        dataRule: {
          videoOriginalName: [
            { required: true, message: '视频原始名称不能为空', trigger: 'blur' }
          ],
          title: [
            { required: true, message: '节点标题不能为空', trigger: 'blur' }
          ],
          courseId: [
            { required: true, message: '课程id不能为空', trigger: 'blur' }
          ],
          catalogId: [
            { required: true, message: '课程目录id不能为空', trigger: 'blur' }
          ],
          videoSourceId: [
            { required: true, message: '云端视频资源id不能为空', trigger: 'blur' }
          ],
          duration: [
            { required: true, message: '播放时长不能为空', trigger: 'blur' }
          ],
          playCount: [
            { required: true, message: '播放次数不能为空', trigger: 'blur' }
          ],
          isFree: [
            { required: true, message: '是否可以试听【0代表免费，1代表收费】不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          modifyTime: [
            { required: true, message: '修改时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/course/video/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.videoOriginalName = data.video.videoOriginalName
                this.dataForm.title = data.video.title
                this.dataForm.courseId = data.video.courseId
                this.dataForm.catalogId = data.video.catalogId
                this.dataForm.videoSourceId = data.video.videoSourceId
                this.dataForm.duration = data.video.duration
                this.dataForm.playCount = data.video.playCount
                this.dataForm.isFree = data.video.isFree
                this.dataForm.createTime = data.video.createTime
                this.dataForm.modifyTime = data.video.modifyTime
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/course/video/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'videoOriginalName': this.dataForm.videoOriginalName,
                'title': this.dataForm.title,
                'courseId': this.dataForm.courseId,
                'catalogId': this.dataForm.catalogId,
                'videoSourceId': this.dataForm.videoSourceId,
                'duration': this.dataForm.duration,
                'playCount': this.dataForm.playCount,
                'isFree': this.dataForm.isFree,
                'createTime': this.dataForm.createTime,
                'modifyTime': this.dataForm.modifyTime
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
